package com.edt.ichannel.api.service.impl;

import cc.edt.iceutils3.json.IceFastJsonUtils;
import com.edt.ichannel.api.common.constant.CommonConstant;
import com.edt.ichannel.api.common.entity.ActionResult;
import com.edt.ichannel.api.dao.InterfaceLogDao;
import com.edt.ichannel.api.entiy.local.InterfaceLog;
import com.edt.ichannel.api.service.InterfaceLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * InterfaceLog服务接口实现类
 * 
 * @author
 * @date 2017-09-15
 */
@Service
public class InterfaceLogServiceImpl implements InterfaceLogService {

	@Resource
	private InterfaceLogDao interfacelogDao;

	/**
	 * 新增保存信息
	 */
	@Override
	public ActionResult saveInterfaceLog(InterfaceLog interfacelog) {
		ActionResult actionResult = new ActionResult();

		interfacelogDao.saveInterfaceLog(interfacelog);
		actionResult.setMessage("添加成功。");
		actionResult.setSuccess(CommonConstant.ACTIONRESULT_TRUE);

		return actionResult;
	}

	/**
	 * 添加日志
	 */
	@Override
	public void addLog(ActionResult result, String paramString, String url, int interfaceType) {
		InterfaceLog log = new InterfaceLog();
		log.setResultInfo(IceFastJsonUtils.toJsonString(result));
		if(paramString == null){
			log.setPostInfo(url);
		}else{
			log.setPostInfo(paramString);
		}
		log.setUrl(url);
		log.setCallDate(new Date());
		log.setInterfaceType(interfaceType);
		log.setCallState(result.isSuccess() ? CommonConstant.CALL_STATE_SUCCESS : CommonConstant.CALL_STATE_FAIL);
		saveInterfaceLog(log);
	}

	/**
	 * 添加日志
	 */
	@Override
	public void addLog(boolean success, String resultStr, String paramString, String url, int interfaceType) {
		InterfaceLog log = new InterfaceLog();
		log.setResultInfo(IceFastJsonUtils.toJsonString(resultStr));
		if(paramString == null){
			log.setPostInfo(url);
		}else{
			log.setPostInfo(paramString);
		}
		log.setUrl(url);
		log.setCallDate(new Date());
		log.setInterfaceType(interfaceType);
		log.setCallState(success ? CommonConstant.CALL_STATE_SUCCESS : CommonConstant.CALL_STATE_FAIL);
		saveInterfaceLog(log);
	}
}